package com.china08.yxyapi.api.manage.sch.platform.users.teacher;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolLessonItem;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicCatalog;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLesson;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsTopicLessonItem;
import com.china08.yxyapi.model.YxyApiPageReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolLessonRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicCatalogRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonItemRepository;
import com.china08.yxyapi.repository.db.YxyLessonsTopicLessonRepository;
import com.china08.yxyapi.util.SchNumGenerator;
import com.china08.yxyapi.util.YxyResUtil;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(tags = { "v01", "教师课程-我的课程" })
@CrossOrigin
@RestController
@RequestMapping(value = "/api_yw")
public class YxyLessonsTeaController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsTopicLessonRepository yxyLessonsTopicLessonRepository;
    @Autowired
    private YxyLessonsTopicCatalogRepository yxyLessonsTopicCatalogRepository;
    @Autowired
    private YxyLessonsTopicLessonItemRepository yxyLessonsTopicLessonItemRepository;
    @Autowired
    private YxyLessonsSchoolLessonRepository yxyLessonsSchoolLessonRepository;
    @Autowired
    private YxyLessonsSchoolLessonItemRepository yxyLessonsSchoolLessonItemRepository;
    @Autowired
    private YxyLessonsSchoolCatalogRepository yxyLessonsSchoolCatalogRepository;

    @SuppressWarnings({ "static-access", "unused" })
    @ApiOperation(value = "教师课程列表")
    @RequestMapping(value = "/manage/tec/lessons/tea/list.v", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> process(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
        String schoolId = getProfile(request, response).getSchoolId();
        String author = getProfile(request, response).getUserNick();
        reqModel.schoolId = schoolId;
        reqModel.author = author;
        Page<YxyLessonsTopicLesson> quePage = null;
        List<RespModel> respModels = new ArrayList<RespModel>();
        long total = 0;
        try {
            if (!"".equals(reqModel.id)) { // 第二次课程
                String[] strings = reqModel.id.split("&");
                if (strings.length > 1) {
                    reqModel.id = strings[0];
                    reqModel.view = "1"; // 查询页面
                }
                YxyLessonsTopicLesson lesson = yxyLessonsTopicLessonRepository.findOne(reqModel.id);
                RespModel rm = new RespModel();
                if (reqModel.view == null) {
                    rm.view = 0; // 创建课程保存页面
                } else {
                    rm.view = 1;
                }
                if (lesson == null) {
                    YxyLessonsSchoolLesson schoolLesson = yxyLessonsSchoolLessonRepository.findOne(reqModel.id);
                    rm.setId(schoolLesson.getId());
                    rm.setCatalogId1(schoolLesson.getCatalogId1());
                    rm.setCatalogId2(schoolLesson.getCatalogId2());
                    YxyLessonsSchoolCatalog cat1 = yxyLessonsSchoolCatalogRepository
                            .findOne(schoolLesson.getCatalogId1());
                    YxyLessonsSchoolCatalog cat2 = yxyLessonsSchoolCatalogRepository
                            .findOne(schoolLesson.getCatalogId2());
                    if (cat1 != null && cat2 != null) {
                        rm.setCatalogName("校本课程" + "-" + cat1.getName() + "-" + cat2.getName());
                        rm.setCatalogName1(cat1.getName());
                        rm.setCatalogName2(cat2.getName());
                    }
                    rm.setName(schoolLesson.getName());
                    rm.setCover(schoolLesson.getCover());
                    rm.setCreateDate(parse(schoolLesson.getCreateDate()));
                    rm.setCount(schoolLesson.getCount());// 总课时
                    if (schoolLesson.getStatus() == 2) {
                        if (schoolLesson.getIsVisible() == 1) {// 0-不显示(下架),1-可显示(上架)
                            rm.setStatus(4);// 状态
                        } else {
                            rm.setStatus(5);// 状态
                        }
                    } else {
                        rm.setStatus(schoolLesson.getStatus());// 状态
                    }
                    // rm.setStatus(schoolLesson.getStatus());// 状态
                    rm.setDuration(schoolLesson.getDuration());// 总时长
                    rm.setDurationName(YxyResUtil.getDurationToString(schoolLesson.getDuration()));
                    // rm.setVip(schoolLesson.getVip());// 是否付费
                    rm.setSummary(schoolLesson.getSummary());
                    rm.setIsVisible(schoolLesson.getIsVisible());
                    rm.setVv(schoolLesson.getVv());
                    rm.setUpDate(parse(schoolLesson.getUpDate()));
                    rm.setDownDate(parse(schoolLesson.getDownDate()));
                    rm.setClzss(1);
                    rm.setPassDate(parse(schoolLesson.getCreateDate()));
                    rm.setRejectType(schoolLesson.getRejectType());
                    rm.setRejectDate(parse(schoolLesson.getRejectDate()));
                    rm.setDownReason(schoolLesson.getDownReason());
                    rm.setDownType(schoolLesson.getDownType());
                    rm.setRejectReason(schoolLesson.getRejectReason());
                    respModels.add(rm);
                } else {
                    rm.setId(lesson.getId());
                    rm.setCatalogId(lesson.getCatalogId3());
                    rm.setCatalogId1(lesson.getCatalogId1());
                    rm.setCatalogId2(lesson.getCatalogId2());
                    YxyLessonsTopicCatalog cat1 = yxyLessonsTopicCatalogRepository.findOne(lesson.getCatalogId1());
                    YxyLessonsTopicCatalog cat2 = yxyLessonsTopicCatalogRepository.findOne(lesson.getCatalogId2());
                    YxyLessonsTopicCatalog cat3 = yxyLessonsTopicCatalogRepository.findOne(lesson.getCatalogId3());
                    if (cat1 != null && cat2 != null && cat3 != null) {
                        rm.setCatalogName("专题课程" + "-" + cat1.getName() + "-" + cat2.getName() + "-" + cat3.getName());
                        rm.setCatalogName1(cat1.getName());
                        rm.setCatalogName2(cat2.getName());
                        rm.setCatelogName3(cat3.getName());
                    }
                    rm.setName(lesson.getName());
                    rm.setCover(lesson.getCover());
                    rm.setCreateDate(parse(lesson.getCreateDate()));
                    rm.setCount(lesson.getCount());// 总课时
                    if (lesson.getStatus() == 2) {
                        if (lesson.getIsVisible() == 1) { // 0-不显示(下架),1-可显示(上架)
                            rm.setStatus(4);// 状态
                        } else {
                            rm.setStatus(5);// 状态
                        }
                    } else {
                        rm.setStatus(lesson.getStatus());// 状态
                    }
                    // rm.setStatus(lesson.getStatus());// 状态
                    rm.setDuration(lesson.getDuration());// 总时长
                    rm.setDurationName(YxyResUtil.getDurationToString(lesson.getDuration()));
                    // rm.setVip(lesson.getVip());// 是否付费
                    rm.setSummary(lesson.getSummary());
                    rm.setIsVisible(lesson.getIsVisible());
                    rm.setVv(lesson.getVv());
                    rm.setClzss(2);
                    rm.setUpDate(parse(lesson.getUpDate()));
                    rm.setDownDate(parse(lesson.getDownDate()));
                    rm.setPassDate(parse(lesson.getCreateDate()));
                    rm.setRejectType(lesson.getRejectType());
                    rm.setRejectReason(lesson.getRejectReason());
                    rm.setDownReason(lesson.getDownReason());
                    rm.setRejectDate(parse(lesson.getRejectDate()));
                    rm.setDownType(lesson.getDownType());
                    respModels.add(rm);
                }

            } else {
                if ((reqModel.status == 4 || reqModel.status == 5)) {// 上架,下架
                                                                     // !"".equals(reqModel.isVisible)
                                                                     // &&
                    if (reqModel.status == 4) { // 已上架 0-不显示(下架),1-可显示(上架)
                        reqModel.isVisible = "1";
                    }
                    if (reqModel.status == 5) { // 已下架
                        reqModel.isVisible = "0";
                    }
                    if (reqModel.clzss == 1) {
                        Page<YxyLessonsSchoolLesson> schPage = null;
                        try {
                            schPage = schQuePage(reqModel, pageable);
                            respModels = schPage(schPage, reqModel);
                        } catch (Exception e) {
                            RespModel rm = new RespModel();
                            rm.setMessage(e.getMessage());
                            respModels.add(rm);
                            e.printStackTrace();
                        }
                        try {
                            total = schPage.getTotalElements();
                        } catch (Exception e) {
                            total = 0L;
                        }
                        // 分页返回
                        Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable,
                                total);
                        return respPage;
                    } else {
                        quePage = topiclessonQuePage(reqModel, pageable);
                    }
                } else if (reqModel.status == 6) {
                    if (reqModel.clzss == 1) {// 校本课程
                        reqModel.schoolId = schoolId;
                        Page<YxyLessonsSchoolLesson> schPage = null, schPage1 = null;
                        try {
                            schPage = keyNameQuePage(reqModel, pageable);
                            respModels = schPage(schPage, reqModel);
                        } catch (Exception e) {
                            RespModel rm = new RespModel();
                            rm.setMessage(e.getMessage());
                            respModels.add(rm);
                            e.printStackTrace();
                        }
                        try {
                            total = schPage.getTotalElements();
                        } catch (Exception e) {
                            total = 0L;
                        }
                        // 分页返回
                        Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable,
                                total);
                        return respPage;
                    } else { // 专题课程
                        quePage = keyNameTopicLessonQuePage(reqModel, pageable);
                        // yxyLessonsTopicLessonRepository.findAll(pageable);
                    }
                } else {// 草稿-待审核
                    if (reqModel.clzss == 1) {// 校本课程
                        Page<YxyLessonsSchoolLesson> schPage = null;
                        try {
                            schPage = schQuePage(reqModel, pageable);
                            respModels = schPage(schPage, reqModel);
                        } catch (Exception e) {
                            RespModel rm = new RespModel();
                            rm.setMessage(e.getMessage());
                            respModels.add(rm);
                            e.printStackTrace();
                        }
                        try {
                            total = schPage.getTotalElements();
                        } catch (Exception e) {
                            total = 0L;
                        }
                        // 分页返回
                        Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable,
                                total);
                        return respPage;
                    } else {
                        quePage = topiclessonQuePage(reqModel, pageable);
                    }
                }

                if (quePage != null) {
                    respModels = respTopicLessonModels(quePage, reqModel);
                }
            }
        } catch (Exception e) {
            RespModel rm = new RespModel();
            rm.setMessage(e.getMessage());
            respModels.add(rm);
            e.printStackTrace();
        }
        try {
            total = quePage.getTotalElements();
        } catch (Exception e) {
            total = 0;
            // e.printStackTrace();
        }
        // 分页返回
        Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable, total);
        return respPage;
    }

    @SuppressWarnings({ "static-access", "unused" })
    @ApiOperation(value = "教师创建课程")
    @RequestMapping(value = "/manage/tec/lessons/tea/create1.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> create(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
        String schoolId = getProfile(request, response).getSchoolId();
        String author = getProfile(request, response).getUserNick();
        List<RespModel> respModels = new ArrayList<RespModel>();
        if (reqModel.clzss == 1) {// 校本课程
            Page<YxyLessonsSchoolLesson> quePage = null;
            try {
                YxyLessonsSchoolLesson les = yxyLessonsSchoolLessonRepository.findBySchoolIdAndName(schoolId,
                        reqModel.name);
                if (les != null) {
                    RespModel rm = new RespModel();
                    rm.setMessage("该课程已存在");
                    respModels.add(rm);
                    Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable, 1);
                    return respPage;
                }
                YxyLessonsSchoolLesson rmm = new YxyLessonsSchoolLesson();
                rmm.setCatalogId1(reqModel.catalogId1);
                rmm.setCatalogId2(reqModel.catalogId2);
                rmm.setName(reqModel.name);
                rmm.setCover(reqModel.cover);
                // rmm.setVip(reqModel.vip);// 是否付费
                rmm.setCount(0);
                // rmm.setVv(1);
                // if (reqModel.vip == 1) {
                // Random random = new Random();// 1-20随机数
                // int rad = random.nextInt(20);
                // if (rad > 0) {
                // rmm.setVv(rad);
                // } else {
                // rmm.setVv(1);
                // }
                // } else {
                // rmm.setVv(0);
                // }
                rmm.setAuthor("云校");
                rmm.setDuration(0);
                rmm.setSummary(reqModel.summary);
                rmm.setCreateDate(new Date());
                rmm.setStatus(0); // 草稿
                rmm.setIsVisible(0);
                // rmm.setSchoolId(schoolId);
                rmm.setPv(0);
                rmm.setAvator("https://yxcsp.91yunxiao.com/yxcsp/pull/2015a95c1c190f2f44272af88e70e59a.jpg");
                yxyLessonsSchoolLessonRepository.saveAndFlush(rmm);
                RespModel rm = new RespModel();
                rm.setId(rmm.getId());
                rm.setCatalogId(rmm.getCatalogId2());
                rm.setName(rmm.getName());
                rm.setCover(rmm.getCover());
                rm.setCreateDate(parse(rmm.getCreateDate()));// 创建时间
                // rm.setVip(rmm.getVip());// 是否付费
                rm.setSummary(rmm.getSummary());
                rm.setStatus(rmm.getStatus());
                respModels.add(rm);
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
        } else {// 专题课程
            Page<YxyLessonsTopicLesson> quePage = null;
            try {
                YxyLessonsTopicLesson les = yxyLessonsTopicLessonRepository.findBySchoolIdAndName(schoolId,
                        reqModel.name);
                if (les != null) {
                    RespModel rm = new RespModel();
                    rm.setMessage("该课程已存在");
                    respModels.add(rm);
                    Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable, 1);
                    return respPage;
                }
                YxyLessonsTopicLesson rmm = new YxyLessonsTopicLesson();
                rmm.setCatalogId1(reqModel.catalogId1);
                rmm.setCatalogId2(reqModel.catalogId2);
                rmm.setCatalogId3(reqModel.catalogId);
                rmm.setName(reqModel.name);
                rmm.setCover(reqModel.cover);
                // rmm.setVip(reqModel.vip);// 是否付费
                rmm.setCount(0);
                // if (reqModel.vip == 1) {
                // Random random = new Random();// 1-20随机数
                // int rad = random.nextInt(20);
                // if (rad > 0) {
                // rmm.setVv(rad);
                // } else {
                // rmm.setVv(1);
                // }
                // } else {
                // rmm.setVv(0);
                // }
                rmm.setAuthor("云校");
                rmm.setSchoolId(schoolId);
                rmm.setDuration(0);
                rmm.setSummary(reqModel.summary);
                rmm.setCreateDate(new Date());
                rmm.setStatus(0); // 草稿
                rmm.setAvator("https://yxcsp.91yunxiao.com/yxcsp/pull/2015a95c1c190f2f44272af88e70e59a.jpg");
                yxyLessonsTopicLessonRepository.saveAndFlush(rmm);
                RespModel rm = new RespModel();
                rm.setId(rmm.getId());
                rm.setCatalogId(rmm.getCatalogId3());
                rm.setName(rmm.getName());
                rm.setCover(rmm.getCover());
                rm.setCreateDate(parse(rmm.getCreateDate()));// 创建时间

                rm.setSummary(rmm.getSummary());
                rm.setStatus(rmm.getStatus());
                respModels.add(rm);
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
        }
        // 分页返回
        Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable, 1);
        return respPage;
    }

    @SuppressWarnings({ "static-access", "unused" })
    @ApiOperation(value = "教师删除课程")
    @RequestMapping(value = "/manage/tec/lessons/tea/delete.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> delete(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
        Page<YxyLessonsTopicLesson> quePage = null;
        List<RespModel> respModels = new ArrayList<RespModel>();
        if (reqModel.clzss == 1) {// 校本课程
            try {
                YxyLessonsSchoolLesson rmm = yxyLessonsSchoolLessonRepository.findOne(reqModel.id);
                if (rmm != null) {
                    if (!"".equals(reqModel.isVisible) && reqModel.status != -1) {// 上架
                        rmm.setIsVisible(Integer.valueOf(reqModel.isVisible));
                    } else {
                        rmm.setIsVisible(0);
                        rmm.setStatus(-1); // 删除状态
                    }
                    yxyLessonsSchoolLessonRepository.saveAndFlush(rmm);
                    RespModel rm = new RespModel();
                    rm.setId(rmm.getId());
                    rm.setCatalogId(rmm.getCatalogId2());
                    rm.setName(rmm.getName());
                    rm.setCover(rmm.getCover());
                    rm.setCreateDate(parse(rmm.getCreateDate()));// 创建时间

                    rm.setSummary(rmm.getSummary());
                    if (rmm.getStatus() == 2) {
                        if (rmm.getIsVisible() == 1) {
                            rm.setStatus(4);
                        } else {
                            rm.setStatus(5);
                        }
                    }
                    rm.setStatus(rmm.getStatus());
                    respModels.add(rm);
                } else {
                    RespModel rm = new RespModel();
                    rm.setId(reqModel.id);
                    rm.setMessage("fail");
                    respModels.add(rm);
                }
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
        } else {
            try {
                YxyLessonsTopicLesson rmm = yxyLessonsTopicLessonRepository.findOne(reqModel.id);
                if (rmm != null) {
                    if (!"".equals(reqModel.isVisible) && reqModel.status != -1) {// 上架
                        rmm.setIsVisible(Integer.valueOf(reqModel.isVisible));
                    } else {
                        rmm.setIsVisible(0);
                        rmm.setStatus(-1); // 删除状态
                    }
                    yxyLessonsTopicLessonRepository.saveAndFlush(rmm);
                    RespModel rm = new RespModel();
                    rm.setId(rmm.getId());
                    rm.setCatalogId(rmm.getCatalogId3());
                    rm.setName(rmm.getName());
                    rm.setCover(rmm.getCover());
                    rm.setCreateDate(parse(rmm.getCreateDate()));// 创建时间

                    rm.setSummary(rmm.getSummary());
                    rm.setStatus(rmm.getStatus());
                    respModels.add(rm);
                } else {
                    RespModel rm = new RespModel();
                    rm.setId(reqModel.id);
                    rm.setMessage("fail");
                    respModels.add(rm);
                }
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
        }
        // 分页返回
        Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable, 1);
        return respPage;
    }

    @SuppressWarnings({ "static-access", "unused" })
    @ApiOperation(value = "教师修改课程")
    @RequestMapping(value = "/manage/tec/lessons/tea/update.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> update(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        Pageable pageable = new PageRequest(reqModel.page, reqModel.size);// 默认时间倒序排列
        Page<YxyLessonsTopicLesson> quePage = null;
        String schoolId = getProfile(request, response).getSchoolId();
        List<RespModel> respModels = new ArrayList<RespModel>();
        if (reqModel.clzss == 1) {// 校本课程
            try {
                YxyLessonsSchoolLesson rmm = yxyLessonsSchoolLessonRepository.findOne(reqModel.id);
                if (rmm != null) {
                    rmm.setCatalogId1(reqModel.catalogId1);
                    rmm.setCatalogId2(reqModel.catalogId2);
                    rmm.setName(reqModel.name);
                    rmm.setCover(reqModel.cover);

                    rmm.setSummary(reqModel.summary);
                    // rmm.setAuthor(reqModel.author);
                    rmm.setSchoolId(schoolId);
                    rmm.setCreateDate(new Date());
                    rmm.setStatus(0); // 草稿
                    yxyLessonsSchoolLessonRepository.saveAndFlush(rmm);
                    RespModel rm = new RespModel();
                    rm.setId(rmm.getId());
                    rm.setCatalogId(rmm.getCatalogId2());
                    rm.setName(rmm.getName());
                    rm.setCover(rmm.getCover());
                    rm.setCreateDate(parse(rmm.getCreateDate()));// 创建时间

                    rm.setSummary(rmm.getSummary());
                    rm.setStatus(rmm.getStatus());
                    respModels.add(rm);
                } else {
                    RespModel rm = new RespModel();
                    rm.setId(reqModel.id);
                    rm.setMessage("fail");
                    respModels.add(rm);
                }
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
        } else {
            try {
                YxyLessonsTopicLesson rmm = yxyLessonsTopicLessonRepository.findOne(reqModel.id);
                if (rmm != null) {
                    rmm.setCatalogId1(reqModel.catalogId1);
                    rmm.setCatalogId2(reqModel.catalogId2);
                    rmm.setCatalogId3(reqModel.catalogId);
                    rmm.setName(reqModel.name);
                    rmm.setCover(reqModel.cover);

                    rmm.setSummary(reqModel.summary);
                    // rmm.setAuthor(reqModel.author);
                    rmm.setSchoolId(schoolId);
                    rmm.setCreateDate(new Date());
                    rmm.setStatus(0); // 草稿
                    yxyLessonsTopicLessonRepository.saveAndFlush(rmm);
                    RespModel rm = new RespModel();
                    rm.setId(rmm.getId());
                    rm.setCatalogId(rmm.getCatalogId3());
                    rm.setName(rmm.getName());
                    rm.setCover(rmm.getCover());
                    rm.setCreateDate(parse(rmm.getCreateDate()));// 创建时间

                    rm.setSummary(rmm.getSummary());
                    rm.setStatus(rmm.getStatus());
                    respModels.add(rm);
                } else {
                    RespModel rm = new RespModel();
                    rm.setId(reqModel.id);
                    rm.setMessage("fail");
                    respModels.add(rm);
                }
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
        }
        // 分页返回
        Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, pageable, 1);
        return respPage;
    }

    @SuppressWarnings({ "static-access", "unused" })
    @ApiOperation(value = "教师课程-上架")
    @Transactional
    @RequestMapping(value = "/manage/tec/lessons/tea/shelf.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> shelf(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        List<RespModel> respModels = new ArrayList<RespModel>();
        if (reqModel.clzss == 1) {// 校本课程
            try {
                YxyLessonsSchoolLesson rmm = yxyLessonsSchoolLessonRepository.findOne(reqModel.id);
                if (rmm != null) {

                    List<YxyLessonsSchoolLessonItem> its = yxyLessonsSchoolLessonItemRepository
                            .findLessonItemsrByLessonId(reqModel.id, 0);
                    if (reqModel.status == 4) {
                        rmm.setIsVisible(1); // 暂时上架状态
                        rmm.setStatus(reqModel.status);
                    }
                    // 待审核 课时状态
                    its.forEach((YxyLessonsSchoolLessonItem itm) -> {
                        if (reqModel.status == 1) {// 待审核
                            if (itm.getStatus() == 0) {
                                itm.setStatus(reqModel.status);
                                yxyLessonsSchoolLessonItemRepository.saveAndFlush(itm);
                            }
                        }
                        if (reqModel.status == 4) {// 上架 审核通过
                            itm.setStatus(reqModel.status);
                            yxyLessonsSchoolLessonItemRepository.saveAndFlush(itm);
                        }
                    });

                    rmm.setCount(its.size()); // 总课时
                    rmm.setStatus(reqModel.status);
                    rmm.setCreateDate(new Date());
                    yxyLessonsSchoolLessonRepository.saveAndFlush(rmm);
                    RespModel rm = new RespModel();
                    rm.setId(rmm.getId());
                    rm.setCatalogId(rmm.getCatalogId2());
                    rm.setName(rmm.getName());
                    rm.setCover(rmm.getCover());
                    rm.setCreateDate(parse(rmm.getCreateDate()));// 创建时间

                    rm.setSummary(rmm.getSummary());
                    rm.setStatus(rmm.getStatus());
                    respModels.add(rm);
                } else {
                    RespModel rm = new RespModel();
                    rm.setId(reqModel.id);
                    rm.setMessage("fail");
                    respModels.add(rm);
                }
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, null, 1);
            return respPage;
        } else {
            try {
                YxyLessonsTopicLesson rmm = yxyLessonsTopicLessonRepository.findOne(reqModel.id);
                if (rmm != null) {

                    List<YxyLessonsTopicLessonItem> its = yxyLessonsTopicLessonItemRepository
                            .findLessonItemsrByLessonId(reqModel.id, 0);
                    if (reqModel.status == 4) {
                        rmm.setIsVisible(1); // 暂时上架状态
                        rmm.setStatus(2);
                    }
                    // 待审核 课时状态
                    its.forEach((YxyLessonsTopicLessonItem itm) -> {
                        if (reqModel.status == 1) {// 待审核
                            if (itm.getStatus() == 0) {
                                itm.setStatus(reqModel.status);
                                yxyLessonsTopicLessonItemRepository.saveAndFlush(itm);
                            }
                        }
                        if (reqModel.status == 4) {// 上架 审核通过
                            itm.setStatus(2);
                            yxyLessonsTopicLessonItemRepository.saveAndFlush(itm);
                        }
                    });
                    rmm.setCount(its.size()); // 总课时
                    // rmm.setStatus(reqModel.status);
                    rmm.setCreateDate(new Date());
                    yxyLessonsTopicLessonRepository.saveAndFlush(rmm);
                    RespModel rm = new RespModel();
                    rm.setId(rmm.getId());
                    rm.setCatalogId(rmm.getCatalogId3());
                    rm.setName(rmm.getName());
                    rm.setCover(rmm.getCover());
                    rm.setCreateDate(parse(rmm.getCreateDate()));// 创建时间

                    rm.setSummary(rmm.getSummary());
                    rm.setStatus(rmm.getStatus());
                    respModels.add(rm);
                } else {
                    RespModel rm = new RespModel();
                    rm.setId(reqModel.id);
                    rm.setMessage("fail");
                    respModels.add(rm);
                }
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, null, 1);
            return respPage;
        }

    }

    @SuppressWarnings({ "static-access", "unused" })
    @ApiOperation(value = "教师课程-下架")
    @RequestMapping(value = "/manage/tec/lessons/tea/offShelf.e", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Page<RespModel> offShelf(HttpServletRequest request, HttpServletResponse response,
            @RequestBody PageReqModel reqModel) throws JsonParseException, JsonMappingException, IOException {
        List<RespModel> respModels = new ArrayList<RespModel>();
        if (reqModel.clzss == 1) {// 校本课程
            try {
                YxyLessonsSchoolLesson rmm = yxyLessonsSchoolLessonRepository.findOne(reqModel.id);
                if (rmm != null) {
                    if (reqModel.status == 5) {
                        rmm.setIsVisible(0); // 暂时上架状态
                        rmm.setStatus(2);
                    }
                    rmm.setDownDate(new Date());
                    yxyLessonsSchoolLessonRepository.saveAndFlush(rmm);
                    RespModel rm = new RespModel();
                    rm.setId(rmm.getId());
                    rm.setCatalogId(rmm.getCatalogId2());
                    rm.setName(rmm.getName());
                    rm.setCover(rmm.getCover());
                    rm.setDownDate(parse(rmm.getDownDate()));// 下架时间

                    rm.setSummary(rmm.getSummary());
                    rm.setStatus(rmm.getStatus());
                    respModels.add(rm);
                } else {
                    RespModel rm = new RespModel();
                    rm.setId(reqModel.id);
                    rm.setMessage("fail");
                    respModels.add(rm);
                }
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, null, 1);
            return respPage;
        } else {
            try {
                YxyLessonsTopicLesson rmm = yxyLessonsTopicLessonRepository.findOne(reqModel.id);
                if (rmm != null) {
                    rmm.setRejectType(reqModel.rejectType);
                    rmm.setRejectReason(reqModel.rejectReason);
                    rmm.setDownDate(new Date());
                    if (reqModel.status == 5) {
                        rmm.setIsVisible(0); // 暂时下架状态
                        rmm.setStatus(2);
                    }
                    yxyLessonsTopicLessonRepository.saveAndFlush(rmm);
                    RespModel rm = new RespModel();
                    rm.setId(rmm.getId());
                    rm.setCatalogId(rmm.getCatalogId3());
                    rm.setName(rmm.getName());
                    rm.setCover(rmm.getCover());
                    rm.setDownDate(parse(rmm.getDownDate()));// 下架时间
                    rm.setIsVisible(0);
                    rm.setSummary(rmm.getSummary());
                    rm.setStatus(rmm.getStatus());
                    respModels.add(rm);
                } else {
                    RespModel rm = new RespModel();
                    rm.setId(reqModel.id);
                    rm.setMessage("fail");
                    respModels.add(rm);
                }
            } catch (Exception e) {
                RespModel rm = new RespModel();
                rm.setMessage(e.getMessage());
                respModels.add(rm);
                e.printStackTrace();
            }
            // 分页返回
            Page<RespModel> respPage = new PageImpl<YxyLessonsTeaController.RespModel>(respModels, null, 1);
            return respPage;
        }
    }

    // 分页请求
    public static class PageReqModel extends YxyApiPageReqModel {
        public String id; // 草稿批次号
        public String catalogId;// 目录id
        public String name;// 课程名称
        public String summary;// 简介
        public String cover;// 封面
        public Integer vip;// 是否vip 0--非vip 1--vip课程
        public String rejectReason; // 下架原因
        public String isVisible; // 0下架 1上架
        public Integer status; // 0草稿
        public Integer clzss; // 课程状态
        public String catalogId1;
        public String catalogId2;
        // public String catalogId3;
        public String rejectType;
        public String author;
        public String schoolId;
        public String view;//
    }

    public static class RespModel extends YxyApiRespModel {
        private String id;
        private String catalogId;// 目录id
        private String catalogId1;// 目录id
        private String catalogId2;// 目录id
        private String name;// 课程名称
        private String summary;// 简介
        private String cover;// 封面
        private Integer vip;// 是否vip 0--非vip 1--vip课程
        private Integer count;// 总课时
        private Integer duration;// 总时长
        private String durationName;// 总时长
        private String createDate;// 创建时间
        private String passDate; // 发布时间
        private String upDate;// 上架时间
        private String downDate;// 下架时间
        private Integer status;// 状态-1-删除,0-草稿,1-待审核,2-审核通过,3-审核拒绝
        private Integer isVisible;// 上架
        private Integer vv;// 学习人数
        private String rejectReason;// 拒绝原因
        private String rejectType;// 拒绝类型
        private String downType;// 下架分类
        private String downReason; // 下架原因
        private String rejectDate;// 拒绝时间
        private String author;// 贡献者
        private String catalogName;// 目录分类
        private Integer clzss; // 1 校本课程 2专题课程
        private String catalogName1;// 目录id
        private String catalogName2;// 目录id
        private String catelogName3;
        public Integer view = 0;

        /**
         * @return the catalogId
         */
        public String getCatalogId() {
            return catalogId;
        }

        /**
         * @param catalogId
         *            the catalogId to set
         */
        public void setCatalogId(String catalogId) {
            this.catalogId = catalogId;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

        /**
         * @param name
         *            the name to set
         */
        public void setName(String name) {
            this.name = name;
        }

        /**
         * @return the summary
         */
        public String getSummary() {
            return summary;
        }

        /**
         * @param summary
         *            the summary to set
         */
        public void setSummary(String summary) {
            this.summary = summary;
        }

        /**
         * @return the cover
         */
        public String getCover() {
            return cover;
        }

        /**
         * @param cover
         *            the cover to set
         */
        public void setCover(String cover) {
            this.cover = cover;
        }

        /**
         * @return the vip
         */
        public Integer getVip() {
            return vip;
        }

        /**
         * @param vip
         *            the vip to set
         */
        public void setVip(Integer vip) {
            this.vip = vip;
        }

        /**
         * @return the count
         */
        public Integer getCount() {
            return count;
        }

        /**
         * @param count
         *            the count to set
         */
        public void setCount(Integer count) {
            this.count = count;
        }

        /**
         * @return the status
         */
        public Integer getStatus() {
            return status;
        }

        /**
         * @param status
         *            the status to set
         */
        public void setStatus(Integer status) {
            this.status = status;
        }

        /**
         * @return the createDate
         */
        public String getCreateDate() {
            return createDate;
        }

        /**
         * @param createDate
         *            the createDate to set
         */
        public void setCreateDate(String createDate) {
            this.createDate = createDate;
        }

        /**
         * @return the id
         */
        public String getId() {
            return id;
        }

        /**
         * @param id
         *            the id to set
         */
        public void setId(String id) {
            this.id = id;
        }

        /**
         * @return the isVisible
         */
        public Integer getIsVisible() {
            return isVisible;
        }

        /**
         * @param isVisible
         *            the isVisible to set
         */
        public void setIsVisible(Integer isVisible) {
            this.isVisible = isVisible;
        }

        /**
         * @return the vv
         */
        public Integer getVv() {
            return vv;
        }

        /**
         * @param vv
         *            the vv to set
         */
        public void setVv(Integer vv) {
            this.vv = vv;
        }

        /**
         * @return the passDate
         */
        public String getPassDate() {
            return passDate;
        }

        /**
         * @param passDate
         *            the passDate to set
         */
        public void setPassDate(String passDate) {
            this.passDate = passDate;
        }

        /**
         * @return the rejectReason
         */
        public String getRejectReason() {
            return rejectReason;
        }

        /**
         * @param rejectReason
         *            the rejectReason to set
         */
        public void setRejectReason(String rejectReason) {
            this.rejectReason = rejectReason;
        }

        /**
         * @return the author
         */
        public String getAuthor() {
            return author;
        }

        /**
         * @param author
         *            the author to set
         */
        public void setAuthor(String author) {
            this.author = author;
        }

        /**
         * @return the catalogId1
         */
        public String getCatalogId1() {
            return catalogId1;
        }

        /**
         * @param catalogId1
         *            the catalogId1 to set
         */
        public void setCatalogId1(String catalogId1) {
            this.catalogId1 = catalogId1;
        }

        /**
         * @return the catalogId2
         */
        public String getCatalogId2() {
            return catalogId2;
        }

        /**
         * @param catalogId2
         *            the catalogId2 to set
         */
        public void setCatalogId2(String catalogId2) {
            this.catalogId2 = catalogId2;
        }

        /**
         * @return the upDate
         */
        public String getUpDate() {
            return upDate;
        }

        /**
         * @param upDate
         *            the upDate to set
         */
        public void setUpDate(String upDate) {
            this.upDate = upDate;
        }

        /**
         * @return the downDate
         */
        public String getDownDate() {
            return downDate;
        }

        /**
         * @param downDate
         *            the downDate to set
         */
        public void setDownDate(String downDate) {
            this.downDate = downDate;
        }

        /**
         * @return the catalogName
         */
        public String getCatalogName() {
            return catalogName;
        }

        /**
         * @param catalogName
         *            the catalogName to set
         */
        public void setCatalogName(String catalogName) {
            this.catalogName = catalogName;
        }

        /**
         * @return the clzss
         */
        public Integer getClzss() {
            return clzss;
        }

        /**
         * @param clzss
         *            the clzss to set
         */
        public void setClzss(Integer clzss) {
            this.clzss = clzss;
        }

        /**
         * @return the rejectType
         */
        public String getRejectType() {
            return rejectType;
        }

        /**
         * @param rejectType
         *            the rejectType to set
         */
        public void setRejectType(String rejectType) {
            this.rejectType = rejectType;
        }

        /**
         * @return the downType
         */
        public String getDownType() {
            return downType;
        }

        /**
         * @param downType
         *            the downType to set
         */
        public void setDownType(String downType) {
            this.downType = downType;
        }

        /**
         * @return the downReason
         */
        public String getDownReason() {
            return downReason;
        }

        /**
         * @param downReason
         *            the downReason to set
         */
        public void setDownReason(String downReason) {
            this.downReason = downReason;
        }

        /**
         * @return the rejectDate
         */
        public String getRejectDate() {
            return rejectDate;
        }

        /**
         * @param rejectDate
         *            the rejectDate to set
         */
        public void setRejectDate(String rejectDate) {
            this.rejectDate = rejectDate;
        }

        /**
         * @return the catalogName1
         */
        public String getCatalogName1() {
            return catalogName1;
        }

        /**
         * @param catalogName1
         *            the catalogName1 to set
         */
        public void setCatalogName1(String catalogName1) {
            this.catalogName1 = catalogName1;
        }

        /**
         * @return the catalogName2
         */
        public String getCatalogName2() {
            return catalogName2;
        }

        /**
         * @param catalogName2
         *            the catalogName2 to set
         */
        public void setCatalogName2(String catalogName2) {
            this.catalogName2 = catalogName2;
        }

        /**
         * @return the catelogName3
         */
        public String getCatelogName3() {
            return catelogName3;
        }

        /**
         * @param catelogName3
         *            the catelogName3 to set
         */
        public void setCatelogName3(String catelogName3) {
            this.catelogName3 = catelogName3;
        }

        /**
         * @return the duration
         */
        public Integer getDuration() {
            return duration;
        }

        /**
         * @param duration
         *            the duration to set
         */
        public void setDuration(Integer duration) {
            this.duration = duration;
        }

        /**
         * @return the durationName
         */
        public String getDurationName() {
            return durationName;
        }

        /**
         * @param durationName
         *            the durationName to set
         */
        public void setDurationName(String durationName) {
            this.durationName = durationName;
        }

    }

    /**
     * 日期格式转换
     * 
     * @param dd
     * @return
     */
    public static String parse(Date date) {
        try {
            SimpleDateFormat tf = new SimpleDateFormat(SchNumGenerator.FORMAT_LONG_YMD);
            return tf.format(date);
        } catch (Exception e) {
            // e.printStackTrace();
            return null;
        }
    }

    public Page<RespModel> quePage() {
        return null;
    }

    public RespModel rmSchLesson(YxyLessonsSchoolLesson lesson, PageReqModel reqModel) {
        RespModel rm = new RespModel();
        rm.setId(lesson.getId());
        rm.setCatalogId1(lesson.getCatalogId1());
        rm.setCatalogId2(lesson.getCatalogId2());
        YxyLessonsSchoolCatalog cat1 = yxyLessonsSchoolCatalogRepository.findOne(lesson.getCatalogId1());
        YxyLessonsSchoolCatalog cat2 = yxyLessonsSchoolCatalogRepository.findOne(lesson.getCatalogId2());
        if (cat1 != null && cat2 != null) {
            rm.setCatalogName("校本课程" + "-" + cat1.getName() + "-" + cat2.getName());
        }
        rm.setName(lesson.getName());
        rm.setCover(lesson.getCover());
        rm.setCreateDate(parse(lesson.getCreateDate()));
        rm.setPassDate(parse(lesson.getCreateDate()));// 发布\上架\下架
        if (lesson.getStatus() == 2) {
            if (lesson.getIsVisible() == 1) {// 0-不显示(下架),1-可显示(上架)
                rm.setUpDate(parse(lesson.getUpDate()));
                rm.setStatus(4);
            } else {
                rm.setDownDate(parse(lesson.getDownDate()));
                rm.setStatus(5);
            }
        } else {
            rm.setStatus(lesson.getStatus());
        }

        // if ("1".equals(reqModel.isVisible)) {
        // rm.setUpDate(parse(lesson.getUpDate()));
        // }
        // if ("0".equals(reqModel.isVisible)) {
        // rm.setDownDate(parse(lesson.getDownDate()));
        // }
        rm.setCount(lesson.getCount());// 总课时
        // rm.setStatus(lesson.getStatus());// 状态
        rm.setDuration(lesson.getDuration());// 总时长
        rm.setDurationName(YxyResUtil.getDurationToString(lesson.getDuration()));
        rm.setVip(lesson.getVip());// 是否付费
        rm.setSummary(lesson.getSummary());
        rm.setIsVisible(lesson.getIsVisible());
        rm.setVv(lesson.getVv());
        rm.setAuthor(lesson.getAuthor());
        rm.setClzss(1);
        return rm;
    }

    public RespModel rmTopicLesson(YxyLessonsTopicLesson lesson, PageReqModel reqModel) {
        RespModel rm = new RespModel();
        rm.setId(lesson.getId());
        rm.setCatalogId(lesson.getCatalogId3());
        rm.setCatalogId1(lesson.getCatalogId1());
        rm.setCatalogId2(lesson.getCatalogId2());
        YxyLessonsTopicCatalog cat1 = yxyLessonsTopicCatalogRepository.findOne(lesson.getCatalogId1());
        YxyLessonsTopicCatalog cat2 = yxyLessonsTopicCatalogRepository.findOne(lesson.getCatalogId2());
        YxyLessonsTopicCatalog cat3 = yxyLessonsTopicCatalogRepository.findOne(lesson.getCatalogId3());
        if (cat1 != null && cat2 != null && cat3 != null) {
            rm.setCatalogName("专题课程" + "-" + cat1.getName() + "-" + cat2.getName() + "-" + cat3.getName());
        }
        rm.setName(lesson.getName());
        rm.setCover(lesson.getCover());
        rm.setCreateDate(parse(lesson.getCreateDate()));
        rm.setPassDate(parse(lesson.getCreateDate()));// 发布\上架\下架
        if (lesson.getStatus() == 2) {
            if (lesson.getIsVisible() == 1) { // 0-不显示(下架),1-可显示(上架)
                rm.setUpDate(parse(lesson.getUpDate()));
                rm.setStatus(4);
            } else {
                rm.setDownDate(parse(lesson.getDownDate()));
                rm.setStatus(5);
            }
        } else {
            rm.setStatus(lesson.getStatus());// 状态
        }
        // if ("1".equals(reqModel.isVisible)) {
        // rm.setUpDate(parse(lesson.getUpDate()));
        // }
        // if ("0".equals(reqModel.isVisible)) {
        // rm.setDownDate(parse(lesson.getDownDate()));
        // }
        rm.setCount(lesson.getCount());// 总课时
        // rm.setStatus(lesson.getStatus());// 状态
        rm.setDuration(lesson.getDuration());// 总时长
        rm.setDurationName(YxyResUtil.getDurationToString(lesson.getDuration()));
        rm.setVip(lesson.getVip());// 是否付费
        rm.setSummary(lesson.getSummary());
        rm.setIsVisible(lesson.getIsVisible());
        rm.setVv(lesson.getVv());
        rm.setAuthor(lesson.getAuthor());
        rm.setClzss(2);
        return rm;
    }

    public List<RespModel> respTopicLessonModels(Page<YxyLessonsTopicLesson> quePage, PageReqModel reqModel) {
        List<RespModel> respModels = new ArrayList<RespModel>();
        quePage.getContent().forEach((YxyLessonsTopicLesson lesson) -> {
            if (lesson.getStatus() == 0 && (lesson.getVip() != null)) {// 云校专题草稿不显示
                // RespModel rModel = rmTopicLesson(lesson, reqModel);
                // respModels.add(rModel);
            } else {
                RespModel rModel = rmTopicLesson(lesson, reqModel);
                respModels.add(rModel);
            }
        });
        return respModels;
    }

    public Page<YxyLessonsTopicLesson> topiclessonQuePage(PageReqModel reqModel, Pageable pageable) {
        Page<YxyLessonsTopicLesson> quePage = null;
        if (!"".equals(reqModel.isVisible) && (reqModel.status == 4 || reqModel.status == 5)) {// 上架,下架
            if (!"".equals(reqModel.catalogId) && !"".equals(reqModel.name.trim())) {
                quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndIsVisibleAndCategoryIdAndNameLike(
                        reqModel.schoolId, Integer.valueOf(reqModel.isVisible), reqModel.catalogId,
                        reqModel.name.trim(), pageable);
            }
            if (!"".equals(reqModel.name.trim()) && "".equals(reqModel.catalogId)) {
                quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndIsVisibleAndNameLike(reqModel.schoolId,
                        Integer.valueOf(reqModel.isVisible), reqModel.name.trim(), pageable);
            }
            if (!"".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
                quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndIsVisibleAndCategoryIdAndNameLike(
                        reqModel.schoolId, Integer.valueOf(reqModel.isVisible), reqModel.catalogId,
                        reqModel.name.trim(), pageable);
            }
            if ("".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
                quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndIsVisible(reqModel.schoolId,
                        Integer.valueOf(reqModel.isVisible), pageable);
            }

        } else {// 草稿 if (reqModel.status == 0)
            if (!"".equals(reqModel.catalogId) && !"".equals(reqModel.name.trim())) {
                quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndStatusAndCategoryIdAndNameLike(
                        reqModel.schoolId, reqModel.status, reqModel.catalogId, reqModel.name.trim(), pageable);
            }
            if (!"".equals(reqModel.name.trim()) && "".equals(reqModel.catalogId)) {
                quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndStatusAndNameLike(reqModel.schoolId,
                        reqModel.status, reqModel.name.trim(), pageable);
            }
            if (!"".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
                quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndStatusAndCategoryIdAndNameLike(
                        reqModel.schoolId, reqModel.status, reqModel.catalogId, reqModel.name.trim(), pageable);
            }
            if ("".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
                quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndStatus(reqModel.schoolId, reqModel.status,
                        pageable);
            }
        }
        return quePage;
    }

    public Page<YxyLessonsSchoolLesson> schQuePage(PageReqModel reqModel, Pageable pageable) {
        Page<YxyLessonsSchoolLesson> schPage = null;
        if (!"".equals(reqModel.isVisible) && (reqModel.status == 4 || reqModel.status == 5)) {// 上架,下架
            if (!"".equals(reqModel.catalogId) && !"".equals(reqModel.name.trim())) {
                schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndIsVisibleAndCategoryIdAndNameLike(
                        reqModel.schoolId, Integer.valueOf(reqModel.isVisible), reqModel.catalogId,
                        reqModel.name.trim(), pageable);
            }
            if (!"".equals(reqModel.name.trim()) && "".equals(reqModel.catalogId)) {
                schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndIsVisibleAndNameLike(reqModel.schoolId,
                        Integer.valueOf(reqModel.isVisible), reqModel.name.trim(), pageable);
            }
            if (!"".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
                schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndIsVisibleAndCategoryIdAndNameLike(
                        reqModel.schoolId, Integer.valueOf(reqModel.isVisible), reqModel.catalogId,
                        reqModel.name.trim(), pageable);
            }
            if ("".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
                schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndIsVisible(reqModel.schoolId,
                        Integer.valueOf(reqModel.isVisible), pageable);
            }

        } else {// 草稿--待审核 if (reqModel.status == 0 || reqModel.status == 1)
            if (!"".equals(reqModel.catalogId) && !"".equals(reqModel.name.trim())) {
                schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndStatusAndCategoryIdAndNameLike(
                        reqModel.schoolId, reqModel.status, reqModel.catalogId, reqModel.name.trim(), pageable);
            }
            if (!"".equals(reqModel.name.trim()) && "".equals(reqModel.catalogId)) {
                schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndStatusAndNameLike(reqModel.schoolId,
                        reqModel.status, reqModel.name.trim(), pageable);
            }
            if (!"".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
                schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndStatusAndCategoryIdAndNameLike(
                        reqModel.schoolId, reqModel.status, reqModel.catalogId, reqModel.name.trim(), pageable);
            }
            if ("".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
                schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndStatus(reqModel.schoolId, reqModel.status,
                        pageable);
            }
        }
        return schPage;
    }

    public Page<YxyLessonsTopicLesson> keyNameTopicLessonQuePage(PageReqModel reqModel, Pageable pageable) {
        Page<YxyLessonsTopicLesson> quePage = null;
        if (!"".equals(reqModel.catalogId) && !"".equals(reqModel.name.trim())) {
            quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndCategoryIdAndNameLike(reqModel.schoolId,
                    reqModel.catalogId, reqModel.name.trim(), pageable);
        }
        if (!"".equals(reqModel.name.trim()) && "".equals(reqModel.catalogId)) {
            quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndNameLike(reqModel.schoolId, reqModel.name.trim(),
                    pageable);
        }
        if (!"".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
            quePage = yxyLessonsTopicLessonRepository.findBySchoolIdAndCategoryIdAndNameLike(reqModel.schoolId,
                    reqModel.catalogId, reqModel.name.trim(), pageable);
        }
        if ("".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
            quePage = yxyLessonsTopicLessonRepository.findAllBySchoolId(reqModel.schoolId, pageable);
        }
        return quePage;
    }

    public Page<YxyLessonsSchoolLesson> keyNameQuePage(PageReqModel reqModel, Pageable pageable) {
        Page<YxyLessonsSchoolLesson> schPage = null;
        if (!"".equals(reqModel.catalogId) && !"".equals(reqModel.name.trim())) {
            schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndCategoryIdAndNameLike(reqModel.schoolId,
                    reqModel.catalogId, reqModel.name.trim(), pageable);
        }
        if (!"".equals(reqModel.name.trim()) && "".equals(reqModel.catalogId)) {
            schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndNameLike(reqModel.schoolId,
                    reqModel.name.trim(), pageable);
        }
        if (!"".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
            schPage = yxyLessonsSchoolLessonRepository.findBySchoolIdAndCategoryIdAndNameLike(reqModel.schoolId,
                    reqModel.catalogId, reqModel.name.trim(), pageable);
        }
        if ("".equals(reqModel.catalogId) && "".equals(reqModel.name.trim())) {
            schPage = yxyLessonsSchoolLessonRepository.findAllBySchoolId(reqModel.schoolId, pageable);
        }
        return schPage;
    }

    public List<RespModel> schPage(Page<YxyLessonsSchoolLesson> schPage, PageReqModel reqModel) {
        List<RespModel> respModels = new ArrayList<RespModel>();
        schPage.getContent().forEach((YxyLessonsSchoolLesson lesson) -> {

            RespModel rModel = rmSchLesson(lesson, reqModel);
            respModels.add(rModel);
        });
        return respModels;
    }

}
